home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / comm / tcp / Crak.lha / crypt.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  3KB  |  113 lines

  1. /*
  2.  * UFC-crypt: ultra fast crypt(3) implementation
  3.  *
  4.  * Copyright (C) 1991, 1992, Free Software Foundation, Inc.
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Library General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU Library General Public
  17.  * License along with this library; if not, write to the Free
  18.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * @(#)crypt.c    2.17 2/22/92
  21.  *
  22.  * Semiportable C version
  23.  *
  24.  */
  25.  
  26. #include "ufc-crypt.h"
  27.  
  28. extern ufc_long *_ufc_dofinalperm();
  29.  
  30. #ifdef _UFC_32_
  31.  
  32. /*
  33.  * 32 bit version
  34.  */
  35.  
  36. extern long32 _ufc_keytab[16][2];
  37. extern long32 _ufc_sb0[], _ufc_sb1[], _ufc_sb2[], _ufc_sb3[];
  38.  
  39. #define SBA(sb, v) (*(long32*)((char*)(sb)+(v)))
  40.  
  41. ufc_long *_ufc_doit(l1, l2, r1, r2, itr)
  42.   ufc_long l1, l2, r1, r2, itr;
  43.   { int i;
  44.     long32 s, *k;
  45.  
  46.     while(itr--) {
  47.       k = &_ufc_keytab[0][0];
  48.       for(i=8; i--; ) {
  49.     s = *k++ ^ r1;
  50.     l1 ^= SBA(_ufc_sb1, s & 0xffff); l2 ^= SBA(_ufc_sb1, (s & 0xffff)+4);  
  51.         l1 ^= SBA(_ufc_sb0, s >>= 16);   l2 ^= SBA(_ufc_sb0, (s)         +4); 
  52.         s = *k++ ^ r2; 
  53.         l1 ^= SBA(_ufc_sb3, s & 0xffff); l2 ^= SBA(_ufc_sb3, (s & 0xffff)+4);
  54.         l1 ^= SBA(_ufc_sb2, s >>= 16);   l2 ^= SBA(_ufc_sb2, (s)         +4);
  55.  
  56.         s = *k++ ^ l1; 
  57.         r1 ^= SBA(_ufc_sb1, s & 0xffff); r2 ^= SBA(_ufc_sb1, (s & 0xffff)+4);  
  58.         r1 ^= SBA(_ufc_sb0, s >>= 16);   r2 ^= SBA(_ufc_sb0, (s)         +4); 
  59.         s = *k++ ^ l2; 
  60.         r1 ^= SBA(_ufc_sb3, s & 0xffff); r2 ^= SBA(_ufc_sb3, (s & 0xffff)+4);  
  61.         r1 ^= SBA(_ufc_sb2, s >>= 16);   r2 ^= SBA(_ufc_sb2, (s)         +4);
  62.       } 
  63.       s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s;
  64.     }
  65.     return _ufc_dofinalperm(l1, l2, r1, r2);
  66.   }
  67.  
  68. #endif
  69.  
  70. #ifdef _UFC_64_
  71.  
  72. /*
  73.  * 64 bit version
  74.  */
  75.  
  76. extern long64 _ufc_keytab[16];
  77. extern long64 _ufc_sb0[], _ufc_sb1[], _ufc_sb2[], _ufc_sb3[];
  78.  
  79. #define SBA(sb, v) (*(long64*)((char*)(sb)+(v)))
  80.  
  81. ufc_long *_ufc_doit(l1, l2, r1, r2, itr)
  82.   ufc_long l1, l2, r1, r2, itr;
  83.   { int i;
  84.     long64 l, r, s, *k;
  85.  
  86.     l = (((long64)l1) << 32) | ((long64)l2);
  87.     r = (((long64)r1) << 32) | ((long64)r2);
  88.  
  89.     while(itr--) {
  90.       k = &_ufc_keytab[0];
  91.       for(i=8; i--; ) {
  92.     s = *k++ ^ r;
  93.     l ^= SBA(_ufc_sb3, (s >>  0) & 0xffff);
  94.         l ^= SBA(_ufc_sb2, (s >> 16) & 0xffff);
  95.         l ^= SBA(_ufc_sb1, (s >> 32) & 0xffff);
  96.         l ^= SBA(_ufc_sb0, (s >> 48) & 0xffff);
  97.  
  98.     s = *k++ ^ l;
  99.     r ^= SBA(_ufc_sb3, (s >>  0) & 0xffff);
  100.         r ^= SBA(_ufc_sb2, (s >> 16) & 0xffff);
  101.         r ^= SBA(_ufc_sb1, (s >> 32) & 0xffff);
  102.         r ^= SBA(_ufc_sb0, (s >> 48) & 0xffff);
  103.       } 
  104.       s=l; l=r; r=s;
  105.     }
  106.  
  107.     l1 = l >> 32; l2 = l & 0xffffffff;
  108.     r1 = r >> 32; r2 = r & 0xffffffff;
  109.     return _ufc_dofinalperm(l1, l2, r1, r2);
  110.   }
  111.  
  112. #endif
  113.